<?php
    session_start();
    $source = $_GET['source'] ?? '';
    $page = $_GET['page'] ?? '';
    if(!$source || ($source <> 'admin' && $source <> 'member')) {
        echo "<script>alert('页面来源错误！');location.href='index.php';</script>";
        exit;
    }
    if(!$page) {    //判断有没有值
        if(is_numeric($page)) { //判断值是不是数字
            echo "<script>alert('页面页数错误！');location.href='index.php';</script>";
            exit;
        }
    }
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>会员管理系统</title>
    <style>
        .main{width:80%; margin:0; text-align:center}
        h2{font-size:16px}
        h2 a{color:navy; text-decoration; none;margin-right:15px}
        h2 a:last-child{margin-right: 0}
        h2 a:hover{color:brown; text-decoration:underline}  /*悬停hover效果 */
        .current{color:darkgreen}
        .red{color: red}
    </style>
</head>
<body>
    <div class="main">
        <?php
            include_once "nav.php";
            include_once "connect.php";
            $username = $_GET['username'] ?? "";
            if($username) { //说明是管理员修改参数
                $sql = "select * from info where username = '$username'";
            } else {
                $sql = "select * from info where username = '".$_SESSION['loginUsername']."'";
            }
            $result = mysqli_query($conn, $sql);
            if(mysqli_num_rows($result)) {
                $info = mysqli_fetch_array($result);
                $fav = explode(",",$info['fav']);
//                print_r($fav);    //查看能否读取到值
            } else {
                die("未找到有效用户");
            }
        ?>
        <form action="postModify.php" method="post" onsubmit="return check()">
            <table align="center" border="1" style="..." cellpadding="5" cellspacing="0">
                <tr>
                    <td align="right">用户名</td>
                    <td align="left"><input name="username" readonly value="<?php echo $info['username'] ?>" ></td>
                </tr>
                <tr>
                    <td align="right">密码</td>
                    <td align="left"><input type="password" placeholder="不修改留空" name="password"></td>
                </tr>
                <tr>
                    <td align="right">确认密码</td>
                    <td align="left"><input type="password" placeholder="不修改留空" name="cpassword"></td>
                </tr>
                <tr>
                    <td align="right">邮箱</td>
                    <td align="left"><input name="email" value="<?php echo $info['email'] ?>"></td>
                </tr>
                <tr>
                    <td align="right">性别</td>
                    <td align="left">
                        <input name="sex" type="radio" <?php if($info['sex']) {?> checked <?php } ?> value="1">男
                        <input name="sex" type="radio" <?php if(!$info['sex']) echo "checked"; ?> value="0">女
                    </td>
                </tr>
                <tr>
                    <td align="right">爱好</td>
                    <td align="left">
                        <input name="fav[]" type="checkbox" <?php if(in_array('听音乐', $fav)) echo "checked"; ?> value="听音乐">听音乐
                        <input name="fav[]" type="checkbox" <?php if(in_array('打蓝球', $fav)) echo "checked"; ?> value="打蓝球">打蓝球
                        <input name="fav[]" type="checkbox" <?php if(in_array('看书', $fav)) echo "checked"; ?> value="看书">看书
                        <input name="fav[]" type="checkbox" <?php if(in_array('玩游戏', $fav)) echo "checked"; ?> value="玩游戏">玩游戏
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" value="提交">
                        <input type="reset" value="重置">
                        <input type="hidden" name="source" value=<?php echo $source ?>>
                        <input type="hidden" name="page" value=<?php echo $page ?>>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <script>
        //前端检测密码、邮箱是否填写正确
        function check() {
            let password = document.getElementsByName('password')[0].value.trim();
            let cpassword = document.getElementsByName('cpassword')[0].value.trim();
            let email = document.getElementsByName('email')[0].value.trim();
            //验证密码
            if(password.length > 0) {
                let passwordRag = /^[a-zA-Z0-9_*]{6,10}$/;
                if(!passwordRag.test(password)) {
                    alert('密码必填，且只能由字母大小写和数字，以及 _ 和 * 构成，长度为3-10个字符');
                    return false;
                } else if(password != cpassword) {
                    alert('两次填写的密码不相同！');
                    return false;
                }
            }

            //邮箱验证
            let emailRag = /^[a-zA-Z0-9_\-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
            if (email.length > 0) {
                if(!emailRag.test(email)) {
                    alert('邮箱格式不正确');
                    return false;
                }
            }
            return true;
        }
    </script>
</body>
</html>